package com.taxshare.purchaser;

import com.taxshare.purchaser.remote.ApiClientConfig;
import com.taxshare.purchaser.remote.ManagerClientConfig;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;

import javax.servlet.MultipartConfigElement;

@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan("com.taxshare")
@MapperScan(basePackages = {"com.taxshare.purchaser.mapper"})
@EnableFeignClients
@EnableAsync
@Slf4j
@Component
@EnableScheduling
@RibbonClients(value = {
        @RibbonClient(name = "tax-manager", configuration = ManagerClientConfig.class),
        @RibbonClient(name = "tax-api", configuration = ApiClientConfig.class)
})
public class PurchaserApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(PurchaserApplication.class, args);
        log.error(">>>>>>>>>Purchaser完美启动>>>>>>>>");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大KB,MB
        factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES));
        //设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.of(10, DataUnit.MEGABYTES));
        //设置默认路径
        factory.setLocation("/tmp");
        return factory.createMultipartConfig();
    }
}
